/**
 * Created by mhy on 2018/8/7.
 */
import * as API from '../../api'
import * as types from '../mutation-types'
const order = {
  state: {
    buysetPackage: [],
    buysetOutFrame: [],
    buysetInFrame: [],
    buysetOutMatcard: [],
    buysetMiddleMatcard: [],
    buysetInMatcard: [],
    buysetManage: [],
    buysetAgent: [],

    buyResult:{}
  },
  getters: {},
  mutations: {
    [types.GET_BUY_SET] (state, data){
      if (data.list){
        state.buysetPackage = data.list.filter(function(item){
          return  item.type == 'multi'
        })
        state.buysetOutFrame = data.list.filter(function(item){
          return  item.type == 'outframe'
        })
        state.buysetInFrame = data.list.filter(function(item){
          return  item.type == 'inframe'
        })
        state.buysetOutMatcard = data.list.filter(function(item){
          return  item.type == 'outmatcard'
        })
        state.buysetMiddleMatcard = data.list.filter(function(item){
          return  item.type == 'middlematcard'
        })
        state.buysetInMatcard = data.list.filter(function(item){
          return  item.type == 'inmatcard'
        })
        state.buysetManage = data.list.filter(function(item){
          return  item.type == 'submanager'
        })
        state.buysetAgent = data.list.filter(function(item){
          return  item.type == 'subagent'
        })

      }
    },
    [types.BUY_VIP] (state,data){
      state.buyResult = data
    }

  },
  actions: {
    getBuySetList({commit, state, rootState}, params){
      var prom = API.getBuySet(params)
      prom.then(function (data) {
        commit(types.GET_BUY_SET, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },
    buyVIP({commit, state, rootState}, params){
      var prom = API.buyvip(params)
      prom.then(function (data) {
        commit(types.BUY_VIP, data.result)
        rootState.reqError = null
      })
        .catch(function (err) {
          rootState.reqError = err
        })
        .finally(function () {
          rootState.reqShake++
        })
      return prom
    },

  }
}
export default order
